<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<div id="notEntityAudioChildrenLayoutConfig-div" class="easyui-window"
	title="音频集合配置" style="width: 96%; height: 96%;"
	data-options="iconCls:'icon-cog',modal:true,openAnimation:'fade',closeAnimation:'slide',cache:false,collapsible:false,minimizable:false,maximizable:false,resizable:false">
	<div id="notEntityAudioChildrenLayoutConfig"
		style="width: 100%; height: 100%;">
		<div data-options="region:'east',split:true,collapsible:false"
			style="height: 30%;" title="子音频列表">
			<div class="cur-not-entity-audio-children-wrapper"
				style="width: 100%; height: 100%;">
				<table id="curNotEntityAudioChildren"
					style="width: 100%; height: 100%;"></table>
			</div>
		</div>
		<div
			data-options="region:'center',split:true,collapsible:false,hideCollapsedContent:false"
			style="height: 70%;" title="音频列表">
			<div class="not-entity-audio-children-wrapper"
				style="width: 100%; height: 100%;">
				<table id="notEntityAudioChildren"
					style="width: 100%; height: 100%;"></table>
			</div>
		</div>

	</div>
</div>


<div id="notEntityAudioChildrenToolBars" style="height: auto;">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-add',plain:true"
		onclick="relationUpdate(true)">添加</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'fa fa-leaf',plain:true"
		onclick="notEntityAudioLayoutTogget(true)">子音频列表</a>|<input
		id="notEntityAudioChildrenAudioTitleQue" style="width: 200px;" />
</div>

<div id="curNotEntityAudioChildrenToolBars" style="height: auto;">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="relationUpdate(false)">解除</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
		onclick="notEntityAudioLayoutTogget(false)">添加</a>
</div>


<script type="text/javascript">
	function notEntityAudioChildrenDatagridReload() {
		try {
			var queryParams = {
				isEntity : true,
				like_title : notEntityAudioChildrenAudioTitleQue
						.textbox('getValue')
			};
			notEntityAudioChildrenDatagrid.datagrid("options").queryParams = queryParams;
			notEntityAudioChildrenDatagrid.datagrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var notEntityAudioChildrenAudioTitleQue = undefined;
	var notEntityAudioChildrenDatagrid = undefined;
	$(function() {

		notEntityAudioChildrenAudioTitleQue = $(
				'#notEntityAudioChildrenAudioTitleQue').textbox({
			buttonText : '标题搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		notEntityAudioChildrenAudioTitleQue.textbox('button').click(function() {
			notEntityAudioChildrenDatagridReload();
		});

		$('.easyui-linkbutton').linkbutton();

		notEntityAudioChildrenDatagrid = $('#notEntityAudioChildren')
				.datagrid(
						{
							url : baseUrl + '/audio/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								isEntity : true
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#notEntityAudioChildrenToolBars',
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										formatter : function(value, row, index) {
											if (row.coverUrl) {
												var img = '<img src="'+row.coverUrl+'" style="width:100px;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										width : 50
									},
									{
										field : 'title',
										title : '标题',
										width : 249
									},
									{
										field : 'sources',
										title : '资源',
										width : 100,
										align : 'left'
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '音频实体',
										width : 50,
										align : 'left',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'parent',
										title : '所属音频集合',
										width : 100,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.parent) {
												return "<span>集合ID:"
														+ row.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.parent.title
														+ "</span>";
											} else {
												return value;
											}
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 50,
										align : 'left',
										sortable : true
									},
									{
										field : 'summary',
										title : '简介',
										width : 100,
										align : 'left',
										editor : {
											type : 'textarea'
										}
									},
									{
										field : 'info',
										title : '详细介绍',
										width : 100,
										align : 'left'
									},
									{
										field : 'playtime',
										title : '播放时间(秒)',
										width : 100,
										align : 'left'
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 80,
										align : 'left',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										}
									},
									{
										field : 'progressStatusCode',
										title : '进度',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										}
									} ] ]
						});

		notEntityAudioChildrenDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							notEntityAudioChildrenDatagrid.datagrid('gotoPage',
									notEntityAudioChildrenDatagrid.datagrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});
	});
</script>


<script type="text/javascript">
	var curNotEntityAudioChildrenDatagrid = undefined;
	var curNotEntityAudioChildEditRowIndex = undefined;
	var notEntityAudioChildrenLayoutConfig = undefined;
	$(function() {
		var notEntityAudioChildrenLayoutConfig_div = $(
				'#notEntityAudioChildrenLayoutConfig-div').window({
			onBeforeClose : function() {

			},
			onClose : function() {
				notEntityAudioChildrenLayoutConfig_div.panel('destroy');
				try {
					audioNotEntityListDatagrid.datagrid('reload');
				} catch (e) {

				}
			}
		});

		notEntityAudioChildrenLayoutConfig = $(
				'#notEntityAudioChildrenLayoutConfig').layout({
			onCollapse : function(region) {
				notEntityAudioChildrenDatagrid.datagrid('reload');
			},
			onExpand : function(region) {
				curNotEntityAudioChildrenDatagrid.datagrid('reload');
			}
		});

		curNotEntityAudioChildrenDatagrid = $('#curNotEntityAudioChildren')
				.datagrid(
						{
							url : baseUrl + '/audio/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								isEntity : true,
								parentId : '${param.itemId}'
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#curNotEntityAudioChildrenToolBars',
							onBeforeSelect : function(index, row) {
								if (curNotEntityAudioChildEditRowIndex != undefined) {
									return false;
								}
							},
							onLoadSuccess : function() {
								curNotEntityAudioChildEditRowIndex = undefined;
							},
							onEndEdit : function(index, row, changes) {
								curNotEntityAudioChildEditRowIndex = undefined;
							},
							onClickCell : function(index, field, value) {
								if (curNotEntityAudioChildEditRowIndex != undefined) {
									if (!curNotEntityAudioChildrenDatagrid
											.datagrid('validateRow',
													curNotEntityAudioChildEditRowIndex)) {
										return;
									}

									$.messager
											.confirm(
													"确认",
													"确定修改排序么?",
													function(r) {
														if (r) {

															var audioSelectRow = curNotEntityAudioChildrenDatagrid
																	.datagrid('getSelected');

															//获取排序
															var _edSort = curNotEntityAudioChildrenDatagrid
																	.datagrid(
																			'getEditor',
																			{
																				index : curNotEntityAudioChildEditRowIndex,
																				field : 'sort'
																			});

															var _Sort = $(
																	_edSort.target)
																	.val();

															var _Data = {
																id : audioSelectRow.id,
																title : audioSelectRow.title,
																sort : _Sort,
																playtime : audioSelectRow.playtime,
																summary : audioSelectRow.summary,
																info : audioSelectRow.info,
																status : audioSelectRow.statusCode
															};

															$
																	.ajax({
																		data : _Data,
																		async : false,
																		type : 'POST',
																		url : baseUrl
																				+ '/audio/update',
																		success : function(
																				data) {
																			if (data.responseCode == '000') {
																				curNotEntityAudioChildrenDatagrid
																						.datagrid(
																								'endEdit',
																								curNotEntityAudioChildEditRowIndex);

																				curNotEntityAudioChildrenDatagrid
																						.datagrid('acceptChanges');

																				$.messager
																						.alert(
																								'提示',
																								'修改成功',
																								'info');
																			} else {
																				$.messager
																						.alert(
																								'提示',
																								data.responseMessage,
																								'error');
																			}
																		}
																	});

														} else {
															curNotEntityAudioChildrenDatagrid
																	.datagrid('rejectChanges');
															curNotEntityAudioChildEditRowIndex = undefined;
														}
													});
									return;
								}

								if (curNotEntityAudioChildEditRowIndex == undefined
										&& field == 'sort') {
									curNotEntityAudioChildrenDatagrid.datagrid(
											'selectRow', index).datagrid(
											'editCell', {
												index : index,
												field : field
											});
									curNotEntityAudioChildEditRowIndex = index;
								}
							},
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										formatter : function(value, row, index) {
											if (row.coverUrl) {
												var img = '<img src="'+row.coverUrl+'" style="width:100px;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										width : 50
									},
									{
										field : 'title',
										title : '标题',
										width : 249
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '音频实体',
										width : 50,
										align : 'left',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 50,
										align : 'left',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'summary',
										title : '简介',
										width : 249,
										align : 'left'
									},
									{
										field : 'info',
										title : '详细介绍',
										width : 249,
										align : 'left'
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 50,
										align : 'left',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										}
									},
									{
										field : 'progressStatusCode',
										title : '进度',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										}
									} ] ]
						});

		curNotEntityAudioChildrenDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							curNotEntityAudioChildrenDatagrid.datagrid(
									'gotoPage',
									curNotEntityAudioChildrenDatagrid.datagrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});

	});

	function relationUpdate(optype) {
		var selectCurChildAudio = curNotEntityAudioChildrenDatagrid
				.datagrid('getSelected');
		if (optype) {
			selectCurChildAudio = notEntityAudioChildrenDatagrid
					.datagrid('getSelected');
		}
		if (selectCurChildAudio) {
			var showMsg = optype ? '添加' : '解除';

			$.messager
					.confirm(
							'确认',
							'您确认想要' + showMsg + '此配置关系吗？',
							function(r) {
								if (r) {
									var _Data = {
										childId : selectCurChildAudio.id,
										parentId : '${param.itemId}',
										optype : optype
									};

									$
											.ajax({
												data : _Data,
												async : false,
												type : 'POST',
												url : baseUrl
														+ '/audio/relation-update',
												success : function(data) {
													if (data.responseCode == '000') {
														$.messager.alert('提示',
																showMsg + '成功',
																'info');
														if (optype) {

														} else {
															$.messager.alert(
																	'提示',
																	'解除成功',
																	'info');
															curNotEntityAudioChildrenDatagrid
																	.datagrid(
																			'deleteRow',
																			curNotEntityAudioChildrenDatagrid
																					.datagrid(
																							'getRowIndex',
																							selectCurChildAudio));
														}
													} else {
														$.messager
																.alert(
																		'提示',
																		data.responseMessage,
																		'error');
													}
												}
											});
								}
							});

		}
	}

	function notEntityAudioLayoutTogget(open) {
		if (open) {
			notEntityAudioChildrenLayoutConfig.layout('expand', 'east');
		} else {
			notEntityAudioChildrenLayoutConfig.layout('collapse', 'east');
		}

	}
</script>



<script type="text/javascript">
	$.extend($.fn.datagrid.methods, {
		editCell : function(jq, param) {
			return jq.each(function() {
				var opts = $(this).datagrid('options');
				var fields = $(this).datagrid('getColumnFields', true).concat(
						$(this).datagrid('getColumnFields'));
				for ( var i = 0; i < fields.length; i++) {
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field) {
						col.editor = null;
					}
				}
				$(this).datagrid('beginEdit', param.index);
				for ( var i = 0; i < fields.length; i++) {
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
	});
</script>